package cn.neusoft.service.impl;

import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.mapper.BaseMapper;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.IBaseService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * BaseService中可以做基础实现
 * 但是模块依然无法确定
 * @param <T>
 */
public class BaseService<T> implements IBaseService<T> {

    @Autowired
    BaseMapper<T> baseMapper;

    @Override
    public ResultBean save(T t) {
        if(t == null){
            return ResultBean.ERROR("参数为空");
        }

        int i = 0;

        /**
         * 1、获取t的字节码文件类
         * 2、获取类名 Business
         * 3、拼接get方法的名字  get+Business+Id
         * 4、通过字节码文件类获取到get方法的对象
         * 5、通过反射调用get方法  获取返回值  id值
         */
        Class<?> clazz = t.getClass();
        String className = clazz.getSimpleName();
        String getMethodName = "get"+className+"Id";
        try {
            Method method = clazz.getMethod(getMethodName);
            Object id = method.invoke(t);

            if(id == null){
                i = baseMapper.add(t);
            }else {
                i = baseMapper.update(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException();
        }

        if(i == 0){
            throw new AppException(AppExceptionCodeMsg.SAVE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean add(T t) {
        if(t == null){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        int i = baseMapper.add(t);

        if(i == 0){
            throw new AppException(AppExceptionCodeMsg.INSERT_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean update(T t) {
        if(t == null){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        int i = baseMapper.update(t);
        if(i == 0) {
            throw new AppException(AppExceptionCodeMsg.UPDATE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean delete(Integer id) {
        if(id == null){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        int i = baseMapper.delete(id);
        if(i == 0) {
            throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean getById(Integer id) {
        if(id == null){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        T t = baseMapper.getById(id);
        if(t == null) {
            throw new AppException(AppExceptionCodeMsg.DATA_NOT_FOUND);
        }
        return ResultBean.SUCCESS();
    }

    @Override
    public ResultBean getAll(BaseQuery baseQuery) {

        if(baseQuery == null){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        //使用分页插件
        Page<T> page = PageHelper.startPage(baseQuery.getPageNow(), baseQuery.getPageCount());

        List<T> list = baseMapper.getAll(baseQuery);

        PageBean<T> pageBean = new PageBean<>();
        pageBean.setTotalRows(page.getTotal());
        pageBean.setData(list);

        return ResultBean.SUCCESS(pageBean);
    }

    @Override
    public ResultBean batchDel(List<Integer> ids) {
        if(ids == null || ids.size() == 0){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }

        for(Integer id : ids){
            int delete = baseMapper.delete(id);
            if(delete == 0){
                throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
            }
        }
        return ResultBean.SUCCESS();
    }
}
